/*
 |--------------------------------------------------------------------------
 | 网络拦截器
 |--------------------------------------------------------------------------
 |
 |
 */


import axios from 'axios'
// import Vue from 'Vue'
import axiosConfig from './axios.config'
// import Loading from 'components/loading'


// let _vLoading
// console.log(vLoading)

import utils from '../utils'
import {
  locationClean
} from 'common/js/cache'
// 创建axios实例
const service = axios.create(axiosConfig);
// if()
let domain = process.domain;
console.log(process.domain,process,'process-----process')
if (process.env.NODE_ENV == 'dev') {
  service.defaults.baseURL = domain;
} else if (process.env.NODE_ENV == 'debug') {
  service.defaults.baseURL = '';
} else if (process.env.NODE_ENV == 'prod') {
  // 判断协议
  let ishttps = 'https:' == document.location.protocol ? true : false;
  if (ishttps) {
    domain = domain.replace("http", "https");
  }
  service.defaults.baseURL = domain
}

// service.defaults.baseURL = 'http://192.168.28.125:7001';
window.sessionStorage.setItem('baseUrl',service.defaults.baseURL)
// 暂存baseUrl
// utils.sessionSet('baseURL',service.defaults.baseURL)
// console.log(utils,'utilsjj')
// let noUseUrl = ['/jyd/accountGetOrderDetailList']
service.interceptors.request.use(config => {
  //   let sign =  noUseUrl.some((v,i)=>{
  //     console.log(config.url,domain + v)
  //     return config.url === domain + v
  //  })
  //  if(!sign){ // 是否使用全局loading
  // Loading.open('请求中...')
  // _vLoading = vLoading()
  // loading.open();
  vProgress.open()
  //  }
  return config;
}, error => {
  Loading.close()
  // Flash('请求超时，请重试')
  console.log(error); // for debug
  Promise.reject(error);
})

// respone拦截器
service.interceptors.response.use(response => {
  // if()
  let {
    code,
    msg
  } = response.data;
  if (code == 10301 || code == 10302) {
    log.warn(msg);
    locationClean('isLogin')
    setTimeout(() => {
      window.location.replace('/admin/index.html#/login');
    }, 1000);

  }
  // console.log(_vLoading.close, '-------response-----')
  // Loading.close()
  // _vLoading.close()
  // loading.close();
  vProgress.close()
  return response;
}, error => {
  // loading.close();
  log.error('响应异常，请重试')
  // alert('xxxxx')
  console.log('error' + error);
  return Promise.reject(error);

})
export default service;
export {
  status
};
